

//
//  WYCStrCardController.m
//  初试
//
//  Created by 中企互联 on 2017/10/13.
//  Copyright © 2017年 李胜军. All rights reserved.
//

#import "WYCStrCardController.h"
#import "WYCStrCardCell.h"
#import "WYCRmtCell.h"
#import "WYCBillModel.h"
@interface WYCStrCardController ()<UITableViewDelegate,UITableViewDataSource>

/**
 *列表
 */
@property (nonatomic,strong)UITableView * tableView;



/** 数组*/
@property(nonatomic,strong)NSMutableArray * resultArray;

/** imagelabel */
@property (nonatomic,strong) UILabel *allImageLabel;

@end
static NSString * ID = @"tag";
@implementation WYCStrCardController

- (void)viewDidLoad {
    [super viewDidLoad];
    //Nav字体颜色 + 背景
    
    [self setupNav];
    
    
    //表视图设置
    
    [self setupTabelView];
    
    //请求
    [self setupRequster];
    
    
    self.resultArray = [[NSMutableArray alloc] init];
    
    
//    [self overalView];
    
}

#pragma make ------------->>请求
- (void)setupRequster{
    NSString * strUrl = [NSString stringWithFormat:@"%@token=%@",creditcardlist,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:strUrl params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue ] == 0) {
            NSArray * resultArray  = responseObject[@"result"];
            for (NSDictionary  * dict in resultArray) {
                WYCBillModel * billModel = [WYCBillModel mj_objectWithKeyValues:dict];
                if ([billModel.isHideCard isEqualToString:@"0"]) {
                    continue;
                }
                [self.resultArray addObject:billModel];
                
            }
            if (self.resultArray.count == 0) {
                [self overalView];
                self.tableView.hidden = YES;
            }else{
                self.allImageLabel.hidden = YES;
                self.tableView.hidden = NO;
            }
            
        }else{
            
            [self showToast:msgStr];
        }
        [self.tableView reloadData];
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [self showToast:Failed];
    }];
    
}


//表视图设置

- (void)setupTabelView{
    
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
    self.tableView.scrollIndicatorInsets = _tableView.contentInset;
    
    CGFloat tableviewY = CGRectGetMaxY(self.navigationController.navigationBar.frame);
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, tableviewY, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
    
}


- (void)overalView{
    
    self.allImageLabel = [[UILabel alloc] init];
    self.allImageLabel.text = @"没有被隐藏的卡片!";
    self.allImageLabel.textAlignment = NSTextAlignmentCenter;
    self.allImageLabel.frame = CGRectMake(0, ScreenHeight / 2, ScreenWidth, 60 * px);
    self.allImageLabel.font = [UIFont systemFontOfSize:50 * px];
    self.allImageLabel.textColor = WYZGrayBg;
    [self.view addSubview:self.allImageLabel];
    
}



//Nav字体颜色+ 背景
- (void)setupNav {
//    self.navigationItem.title = @"隐藏卡片管理";
//    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
//    [self.navigationController.navigationBar setBarTintColor:WYCMain];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.resultArray.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 240 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    /** 分割线72*px */
    //    tableView.separatorColor = WYZGrayBg;
//    //    tableView.separatorInset = UIEdgeInsetsMake(0,72*px, 0, 72*px);        // 设置端距，这里表示separator离左边和右边均80像素
//    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
    
    WYCStrCardCell * cell = [WYCStrCardCell cellWithTableView:tableView];
    cell.billmodel = self.resultArray[indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    if([cell.billmodel.isHideCard isEqualToString:@"0"]) {
        self.tableView.hidden = YES;
    }else{
        
        self.allImageLabel.hidden = YES;
        self.tableView.hidden = NO;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    WYCBillModel  *billmodel = self.resultArray[indexPath.row];
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"token"] = [WYCAccountTool unarchivetoken];
    params[@"cardId"] = billmodel.cardId;
    params[@"isHideCard"] = @"0";
    [HttpTool POST:creditcardsetHideCard params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            
            [self showToast:@"成功"];
            [self.navigationController popViewControllerAnimated:YES];
            [self.tableView reloadData];
            
        }else{
            
            [self showToast:msgStr];
            
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
        [self showToast:Failed];
        
    }];
    
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    
    
    self.navigationController.navigationBar.hidden = YES;
    
    
    UIView * NavView = [[UIView alloc] init];
    NavView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    NavView.backgroundColor = WYCmainColor;
    [self.view addSubview:NavView];
    
    
    
    CALayer *bottomLineLayer = [[CALayer alloc] init];
    bottomLineLayer.frame = CGRectMake(0, NavView.bounds.size.height - 0.5, NavView.bounds.size.width, 0.5);
    bottomLineLayer.backgroundColor = WYCmainColor.CGColor;
    [NavView.layer addSublayer:bottomLineLayer];
    
    
    
    
    
    //返回按钮
    UIButton *  fanhuibutton = [[UIButton alloc] init];
    [fanhuibutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [fanhuibutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    fanhuibutton.frame = CGRectMake(30 * px, titleY, 66 * px , 66 * px);
    [NavView addSubview:fanhuibutton];
    
    
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(0, titleY, NavView.width, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"隐藏卡片管理";
    [NavView addSubview:NavRigsterLabel];
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
